import com.webservice.factory.SystemFactory;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeTest {

    @Test
    public void test() throws ParseException {
//        Date date = new Date("2020-08-09");
//        System.out.println(date);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sf.parse("2020-08-09");
        System.out.println( date1);
    }

    @Test
    public void test1(){
        DateTime dateTime = new DateTime();
        System.out.println("Using system clock: " + dateTime.toString());
        System.out.println(dateTime.getZone());
        dateTime = SystemFactory.getClock().getDateTime();
        System.out.println("Using (better) system clock: " + dateTime.toString());

    }

    @Test
    public void test2(){
        Date date = new Date();
        DateTime dateTime = new DateTime();
        System.out.println(dateTime);
        System.out.println(dateTime.getMillis());
        date = dateTime.toDate();
        System.out.println(date);
    }

    @Test
    public void test3(){
        Date date = new Date();
        DateTime dateTime = new DateTime(date.getTime());
        System.out.println(dateTime);
    }

    @Test
    public void example1_1() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(2000, Calendar.JANUARY, 1, 0, 0, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("E MM/dd/yyyy HH:mm:ss.SSS");
        calendar.add(Calendar.DAY_OF_MONTH, 90);
        System.out.println(sdf.format(calendar.getTime()));
    }

    @Test
    public void example1_2() {
        DateTime dateTime = new DateTime(2000, 1, 1, 0, 0, 0, 0);
        System.out.println(dateTime.plusDays(90).toString("E MM/dd/yyyy HH:mm:ss.SSS"));
    }

    @Test
    public void example1_3() {
        DateTime dt = new DateTime(2000, 1, 1, 0, 0, 0, 0);
        System.out.println(
                dt.plusDays(45)
                        .plusMonths(1)
                        .dayOfWeek()
                        .withMaximumValue()
                        .toString("E MM/dd/yyyy HH:mm:ss.SSS"));
    }

    @Test
    public void example1_4() {
        Calendar calendar = Calendar.getInstance();
        DateTime dateTime = new DateTime(2000, 1, 1, 0, 0, 0, 0);
        System.out.println(dateTime.plusDays(45).plusMonths(1).dayOfWeek()
                .withMaximumValue().toString("E yyyy/MM/dd HH:mm:ss.SSS"));
        calendar.setTime(dateTime.toDate());
    }

    @Test
    public void example3_1() {
        LocalDate now = SystemFactory.getClock().getLocalDate();
        System.out.println("Now:" + now.toString());
        LocalDate lastDayOfPreviousMonth = now.minusMonths(1).dayOfMonth().withMaximumValue();
        System.out.println("Then: " + lastDayOfPreviousMonth.toString());
    }
}
